#region Imported Namespaces


using System;
using System.Globalization;
using System.Windows.Data;

using WtfManager.Framework;
using WtfManager.HierarchicalData;


#endregion


namespace WtfManager.FileManager
{
  ///<summary>
  /// Converts Data hierarchy <see cref="TreeItem">tree items</see> to a path string and back.
  ///</summary>
  [ValueConversion(typeof(FileSystemItem), typeof(String))]
  public class FileSystemItemToPathConverter: Service, IValueConverter
  {
    #region Fields


    private static FileSystemItemToPathConverter _instance;


    #endregion


    #region Constructors/Finalizers


    ///<summary>
    /// Default FileSystemItemToPathConverter constructor.
    ///</summary>
    internal FileSystemItemToPathConverter()
    {
    }


    #endregion


    #region Properties/Indexers


    public override string Description
    {
      get
      {
        return "Converts File System Items to path strings.";
      }
    }


    private static FileSystem FileSystemService
    {
      get
      {
        return (FileSystem)WtfFramework.Instance.Services[typeof(FileSystem)];
      }
    }

    ///<summary>
    /// Gets an instance of <see cref="TreeItemToPathConverter"/>
    ///</summary>
    public static FileSystemItemToPathConverter Instance
    {
      get
      {
        return _instance;
      }
    }


    public override string Name
    {
      get
      {
        return "File System Item To Path Converter";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      _instance = IsInitialized ? this : null;
    }


    ///<summary>
    /// Converts a value. 
    ///</summary>
    ///<returns>A converted value. If the method returns null, the valid null value is used.</returns>
    ///<param name="value">The value produced by the binding source.</param>
    ///<param name="targetType">The type of the binding target property.</param>
    ///<param name="parameter">The converter parameter to use.</param>
    ///<param name="culture">The culture to use in the converter.</param>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var item = value as FileSystemItem;

      return item != null ? item.FileSystemPath : null;
    }


    ///<summary>
    ///Converts a value. 
    ///</summary>
    ///<returns>A converted value. If the method returns null, the valid null value is used.</returns>
    ///<param name="value">The value that is produced by the binding target.<param>
    ///<param name="targetType">The type to convert to.</param>
    ///<param name="parameter">The converter parameter to use.</param>
    ///<param name="culture">The culture to use in the converter.</param>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var path = value as string;

      if (string.IsNullOrEmpty(path))
      {
        return null;
      }

      return FileSystemService == null ? null : FileSystemService.MyComputer.GetItem(path);
    }


    protected override FrameworkComponentState OnLoad()
    {
      AddDependency(typeof(FileSystem));

      return FrameworkComponentState.Loaded;
    }


    protected override void OnUnload()
    {
      _instance = null;
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    #endregion
  }
}